package com.ssm.studen.service.impl;

import com.ssm.studen.dao.AnswerDao;
import com.ssm.studen.dao.ExampaperanswerDao;
import com.ssm.studen.model.Answer;
import com.ssm.studen.model.Exampaperanswer;
import com.ssm.studen.service.ExampaperanswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 试卷答题表：
 *          实现业务及流程接口当中的内容
 */
@Service
public class ExampaperanswerServiceImpl implements ExampaperanswerService {

    @Autowired
    private ExampaperanswerDao exampaperanswerDao;
    @Autowired
    private AnswerDao answerDao;

    /**
     * 试卷答题表查询
     * @param epanum
     * @return
     */
    @Override
    public List<Exampaperanswer> selectExampaperanswer(int epanum) {
        List<Exampaperanswer> list = exampaperanswerDao.selectExampaperanswer(epanum);
        return list;
    }


    /**
     * 试卷答题表添加
     * @param exampaperanswer
     * @return
     */
    @Override
    public int insertExampaperanswer(Exampaperanswer exampaperanswer) {
        int result = 0;
        //1.通过答题表主键判断是否存在试卷答题表
        int answerIdCount = answerDao.answerIdCount(new Answer().getAnum());
        if(answerIdCount<1){
            //2.添加试卷答题表
            int addExampaperanswer = exampaperanswerDao.insertExampaperanswer(exampaperanswer);
            //3.返回值设置
            result = addExampaperanswer;
        }else{
            //存在此表
            result = -1;
        }
        return result;
    }


    /**
     * 试卷答题表修改更新
     * @param exampaperanswer
     * @return
     */
    @Override
    public int updateExampaperanswer(Exampaperanswer exampaperanswer) {
        /*
         * 业务流程判断
         * */
        int isUpdate = 0;
        try {
            isUpdate = exampaperanswerDao.updateExampaperanswer(exampaperanswer);
        }catch (Exception e){
            isUpdate = -1;
            //打印更新时报错的情况
            System.out.println("更新试卷答题表信息时执行SQL报错的原因是：" + e.getMessage());
        }
        return isUpdate;
    }
}
